package com.taotao.service.ipml;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.pojo.TreeNode;
import com.taotao.mapper.TbContentCategoryMapper;
import com.taotao.pojo.TbContentCategory;
import com.taotao.pojo.TbContentCategoryExample;
import com.taotao.pojo.TbContentCategoryExample.Criteria;
import com.taotao.service.IContentCategoryService;

/**
 * 内容分类管理
 * 
 * @author Administrator
 *
 */
@Service
public class ContentCategoryServiceIpml implements IContentCategoryService {

	@Autowired
	private TbContentCategoryMapper contentCategoryMapper;

	/**
	 * 获取内容分类
	 */
	@Override
	public List<TreeNode> getContentCategory(Long parentId) {
		TbContentCategoryExample example = new TbContentCategoryExample();
		Criteria criteria = example.createCriteria();
		// 根据parentId进行查询
		criteria.andParentIdEqualTo(parentId);
		List<TbContentCategory> contentCategories = this.contentCategoryMapper
				.selectByExample(example);

		List<TreeNode> treeNodes = new ArrayList<TreeNode>();
		for (TbContentCategory tbContentCategory : contentCategories) {
			long id = tbContentCategory.getId();
			String text = tbContentCategory.getName();
			String state = tbContentCategory.getIsParent() ? "closed" : "open";
			TreeNode treeNode = new TreeNode(id, text, state);
			treeNodes.add(treeNode);
		}
		return treeNodes;
	}

	/**
	 * 保存内容分类节点
	 */
	public TaotaoResult createContentCategory(TbContentCategory contentCategory) {
		contentCategory.setStatus(1);
		contentCategory.setIsParent(false);
		Date date = new Date();
		contentCategory.setCreated(date);
		contentCategory.setUpdated(date);
		long id = this.contentCategoryMapper.insert(contentCategory);
		TbContentCategory _contentCategory = this.contentCategoryMapper
				.selectByPrimaryKey(contentCategory.getParentId());
		if (!_contentCategory.getIsParent()) {
			_contentCategory.setIsParent(true);
			this.contentCategoryMapper.updateByPrimaryKey(_contentCategory);
		}
		return TaotaoResult.ok(contentCategory);
	}

	/**
	 * 通过id删除内容目录结点
	 */
	@Override
	public TaotaoResult deleteContentCategoryById(Long id) {
		// 通过id查找内容分类结点
		TbContentCategory contentCategory = this.contentCategoryMapper
				.selectByPrimaryKey(id);
		// 通过id删除内容分类节点
		this.contentCategoryMapper.deleteByPrimaryKey(id);
		TbContentCategoryExample example = new TbContentCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(contentCategory.getParentId());
		List<TbContentCategory> contentCategories = this.contentCategoryMapper
				.selectByExample(example);
		// 判断删除之后是否为父节点
		if (contentCategories != null && contentCategories.size() > 0) {
			contentCategory.setIsParent(false);
			// 修改内容分类节点
			this.contentCategoryMapper.updateByPrimaryKey(contentCategory);
		}
		return TaotaoResult.ok();
	}

	/**
	 * 通过id删除内容管理结点
	 */
	@Override
	public TaotaoResult renameContentCategoryById(Long id, String name) {
		// TODO Auto-generated method stub
		TbContentCategory contentCategory = this.contentCategoryMapper
				.selectByPrimaryKey(id);
		String olderName = contentCategory.getName();
		if (!olderName.equals(name)) {
			contentCategory.setName(name);
			this.contentCategoryMapper.updateByPrimaryKey(contentCategory);
		}
		return TaotaoResult.ok();
	}
}
